<template>
	<view class="login-contailner">
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<button type="primary" class="btn-login" @click="bindGetUserInfo">一键登录</button>
		<text class="tips-text">登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed:{
			...mapState('user',['redirectinfo'])
		},
		methods:{
			...mapMutations('user',['updateUserInfo','updateToken','updateRedirectInfo']),
			bindGetUserInfo(){
				uni.getUserProfile({
					desc:"获取您的称昵，头像，地区及性别",
					success:(res)=>{
						//console.log(res)
						this.updateUserInfo(res.userInfo)
						this.getToken(res)
					}
				})
			},
			async getToken(info){
				
				const [err,res]=await uni.login().catch(err=>err)
				
				if(err||res.errMsg !=='login:ok') return uni.$showMsg('登录失败！')
				
				const query={
					code:res.code,
					encryptedData:info.encryptedData,
					iv:info.iv,
					rawData:info.rawData,
					signature:info.signature
				}
				
				const {data:loginResult}= await uni.$http.post('/api/public/v1/users/wxlogin',query)
				console.log(loginResult)
				this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
				//if(loginResult.meta.status!==200) return uni.$showMsg('登录失败！')
				
				//this.updateToken(loginResult.message.token)
				this.NavigateBack()
			},
			NavigateBack(){
				if(this.redirectinfo && this.redirectinfo.openType==='switchTab'){
					uni.switchTab({
						url:this.redirectinfo.from,
						complete:()=>{
							this.updateRedirectInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss"> 
.login-contailner{
	height: 750rpx;
	background-color: #F8F8F8;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	position: relative;
	overflow: hidden;//溢出部分隐藏
	&::after{
		content: ' ';
		border-radius: 100%;
		display:block;
		width: 100%;
		height: 40px;
		background-color: white;
		position: absolute;
		bottom: 0;
		left: 0;
		transform: translateY(50%);//向下溢出50%
	}
	.contact-filled{}
	.btn-login{
		width: 90%;
		border-radius: 100px;
		background-color: #C00000;
		margin: 15px 0;
	}
	.tips-text{
		font-size: 12px;
		color: grey;
	}
}
</style>